package com.michale.framework.security.service.impl;

import com.michale.common.domain.permissions.RoleEntity;
import com.michale.common.domain.security.LoginUserDetail;
import com.michale.common.utils.String.StringUtils;
import com.michale.common.utils.security.SecurityUtils;
import com.michale.framework.security.context.PermissionContextHolder;
import com.michale.framework.security.service.MyPermissionService;
import org.springframework.stereotype.Service;

import java.util.Set;

/**
 * @author Michale
 * @apiNote 自定义权限
 */
@Service("security")
public class MyPermissionServiceImpl implements MyPermissionService {

    /**
     * 所有权限标识
     */
    private static final String ALL_PERMISSION = "*:*:*";

    /**
     * 管理员角色权限标识
     */
    private static final String SUPER_ADMIN = "admin";

    private static final String ROLE_DELIMITER = ",";

    private static final String PERMISSION_DELIMITER = ",";

    /**
     * 判断是否包含权限
     *
     * @param permissions 权限列表
     * @param permission  权限字符串
     * @return 用户是否具备某权限
     */
    public boolean hasPermissions(Set<String> permissions, String permission) {
        return permissions.contains(ALL_PERMISSION) || permissions.contains(StringUtils.trim(permission));
    }

    /**
     * 验证用户是否具备某权限
     *
     * @param permission 权限字符串
     * @return 用户是否具备某权限
     */
    @Override
    public boolean isPermission(String permission) {
        if (StringUtils.isEmpty(permission)) {
            return false;
        }
        LoginUserDetail loginUser = SecurityUtils.getLoginUser();
        if (StringUtils.isNull(loginUser) || StringUtils.isEmpty(loginUser.getPermissions())) {
            return false;
        }
        PermissionContextHolder.setContext(permission);
        return hasPermissions(loginUser.getPermissions(), permission);
    }

    /**
     * 验证用户是否不具备某权限
     *
     * @param permission 权限字符串
     * @return 用户是否不具备某权限
     */
    @Override
    public boolean notPermission(String permission) {
        return isPermission(permission);

    }

    /**
     * 判断用户是否拥有某个角色
     *
     * @param permission 角色字符串
     * @return 用户是否拥有某个角色
     */
    @Override
    public boolean hasRole(String permission) {
        if (StringUtils.isEmpty(permission)) {
            return false;
        }
        LoginUserDetail loginUser = SecurityUtils.getLoginUser();
        if (StringUtils.isNull(loginUser) || StringUtils.isEmpty(loginUser.getUserEntity().getRoleEntityList())) {
            return false;
        }
        for (RoleEntity sysRole : loginUser.getUserEntity().getRoleEntityList()) {
            String roleKey = sysRole.getRoleKey();
            if (SUPER_ADMIN.equals(roleKey) || roleKey.equals(StringUtils.trim(permission))) {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断用户是否不拥有某个角色
     *
     * @param permission 角色字符串
     * @return boolean
     */
    @Override
    public boolean notHasRole(String permission) {
        return hasRole(permission);

    }

}
